package com.it.gw.modules.water.activity.douqu

import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.activity.viewModels
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import com.it.common.bean.MapDataBean
import com.it.common.utils.GsonUtils
import com.it.common.utils.isNotEmpty
import com.it.data.network.req.DouQRecordDto
import com.it.gw.R
import com.it.gw.base.BaseActivity
import com.it.gw.databinding.ActivityDouquItemDetailBinding
import com.it.gw.dialog.ProgressDialog
import com.it.gw.modules.map.activity.MapActivity
import com.it.gw.modules.water.viewmodel.WaterViewModel

//斗渠详情
class DouQuItemDetailActivity : BaseActivity<ActivityDouquItemDetailBinding>() {
    private val waterViewModel by viewModels<WaterViewModel>()

    companion object {
        const val TAG = "DouQuItemDetailActivity"
    }


    override fun initObserver() {
        waterViewModel.douQRecordDtoDetailLiveData.observe(this) {
            ProgressDialog.hide()
            Log.d(TAG, "douQRecordDtoDetailLiveData: ${it}")
            if (it.result && it.data.isNotEmpty()) {
                setDetailView(it.data!!)
            }

        }
        waterViewModel.insertLiveData.observe(this) {
            ProgressDialog.hide()
            if (it.result) {
                Toast.makeText(this, "操作成功", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "${it.msg}", Toast.LENGTH_SHORT).show()
            }

        }

    }

    private fun setDetailView(data: DouQRecordDto) {
        binding.lttd.setText(data.lttd) // 泵站纬度
        binding.douqCode.setText(data.douqCode) // 工程编号
        binding.qdmc.setText(data.qdmc) // 渠道名称
        binding.sztcdm.setText(data.sztcdm) // 所在团场
        binding.ld.setText(data.ld) // 连队
        binding.kzggmj.setText(data.kzggmj) // 控制灌溉面积(亩)
        binding.dmxs.setText(data.dmxs) // 断面形式
        binding.fscd.setText(data.fscd) // 防渗长度(km)
        binding.fsl.setText(data.fsl) // 防渗率(%)
        binding.jzwslXj.setText(data.jzwslXj) // 建筑物数量(座)
        binding.jzwslZ.setText(data.jzwslZ) // 闸(座)
        binding.jzwslQ.setText(data.jzwslQ) // 桥(座)
        binding.jzwslH.setText(data.jzwslH) // 涵(座)
        binding.jzwslDhx.setText(data.jzwslDhx) // 倒虹吸(座)
        binding.jzwslDs.setText(data.jzwslDs) // 跌水(座)
        binding.jzwslClq.setText(data.jzwslClq) // 测流桥(座)
        binding.jzwslSd.setText(data.jzwslSd) // 隧洞(座)
        binding.jzwslDc.setText(data.jzwslDc) // 渡槽(座)
        binding.zjgjnf.setText(data.zjgjnf) // 最近改建年份
        binding.gldwmc.setText(data.gldwmc) // 管理单位名称
        binding.lgtd.setText(data.lgtd) // 经度
        binding.note.setText(data.note) // 更新备注

    }


    override fun initListener() {

        binding.lgtd.setOnClickListener {
            startLocation()
        }
        binding.lttd.setOnClickListener {
            startLocation()
        }

        binding.btnSave.setOnClickListener {
            insertDouQRecord()

        }


        binding.zjgjnf.setOnClickListener {
            showYearPickerDialog(selectYearAction = {
                binding.zjgjnf.text = it.toString()
            })
        }

    }

    private fun insertDouQRecord() {
        ProgressDialog.show(this)
        val dto = DouQRecordDto(
            lgtd = binding.lgtd.text.toString(), // 泵站经度
            douqCode = binding.douqCode.text.toString(), // 工程编号
            qdmc = binding.qdmc.text.toString(), // 渠道名称
            sztcdm = binding.sztcdm.text.toString(), // 所在团场
            ld = binding.ld.text.toString(), // 连队
            kzggmj = binding.kzggmj.text.toString(), // 控制灌溉面积(亩)
            dmxs = binding.dmxs.text.toString(), // 断面形式
            fscd = binding.fscd.text.toString(), // 防渗长度(km)
            fsl = binding.fsl.text.toString(), // 防渗率(%)
            jzwslXj = binding.jzwslXj.text.toString(), // 建筑物数量(座)
            jzwslZ = binding.jzwslZ.text.toString(), // 闸(座)
            jzwslQ = binding.jzwslQ.text.toString(), // 桥(座)
            jzwslH = binding.jzwslH.text.toString(), // 涵(座)
            jzwslDhx = binding.jzwslDhx.text.toString(), // 倒虹吸(座)
            jzwslDs = binding.jzwslDs.text.toString(), // 跌水(座)
            jzwslClq = binding.jzwslClq.text.toString(), // 测流桥(座)
            jzwslSd = binding.jzwslSd.text.toString(), // 隧洞(座)
            jzwslDc = binding.jzwslDc.text.toString(), // 渡槽(座)
            zjgjnf = binding.zjgjnf.text.toString(), // 最近改建年份
            gldwmc = binding.gldwmc.text.toString(), // 管理单位名称
            lttd = binding.lttd.text.toString(), // 纬度
            note = binding.note.text.toString() // 更新备注
        )
        waterViewModel.insertDouQRecord(dto)

    }

    override fun initData() {
        ProgressDialog.show(this)
        getData()
    }

    private fun getData() {
        waterViewModel.getDouQBasic(intent.getStringExtra("id")!!)

    }


    var title = ""
    override fun initView() {
        title = "${intent.getStringExtra("title")}"
        setTitle(
            title = title,
            isBack = true,
            tvRightRes1 = R.string.str_location,
            tvRight1Action = {
                val intent = Intent(this, MapActivity::class.java)
                intent.putExtra("title", title)
                val mapList = mutableListOf(
                    MapDataBean(
                        name = title,
                        lgtd = binding.lgtd.text.toString(),
                        lttd = binding.lttd.text.toString(),
                    )
                )
                intent.putExtra(MapActivity.MAP_DATA, GsonUtils.objToJsonStr(mapList))
                startActivity(intent)
            }

        )

    }

    override fun setContentView() {
        binding = ActivityDouquItemDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)
    }

    //声明AMapLocationClientOption对象
    private var mLocationOption: AMapLocationClientOption? = null

    //声明AMapLocationClient类对象
    private var mLocationClient: AMapLocationClient? = null
    private fun startLocation() {
        Log.d(TAG, "startLocation: ===========")
        mLocationClient = AMapLocationClient(applicationContext)
        mLocationClient?.setLocationListener(object : AMapLocationListener {
            override fun onLocationChanged(p0: AMapLocation?) {
                val longitude = "${p0?.longitude}"
                val latitude = "${p0?.latitude}"
                Log.d(TAG, "onLocationChanged: ${longitude} ${latitude}")
                binding.lttd.setText(latitude)
                binding.lgtd.setText(longitude)

            }

        })

        mLocationOption = AMapLocationClientOption()
        val option = AMapLocationClientOption()
        option.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn)
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        option.setOnceLocation(true);
        option.setOnceLocationLatest(true);

        if (null != mLocationClient) {
            mLocationClient?.setLocationOption(option)
            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
            mLocationClient?.stopLocation()
            mLocationClient?.startLocation()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        mLocationClient?.onDestroy();//销毁定位客户端，同时销毁本地定位服务。
    }


}